$(document).ready(function() {
	
		
				
		//$('.date_pick').datePicker({startDate:'01/01/2009'});
		addDatePicker("#date_dd",'#date_mm','#date_yyyy',true,true,true);
		
		$('#duplicated_record').click(function() {
			$('#status_message').hide();
			if (!checkMonth($('#date_mm'),true,'','status_message')) {
				$('#status_message').show();
				return;
			} 
			
			if (!checkDay($('#date_dd'),true,'','status_message')) {
				$('#status_message').show();	
				return;
			}	
			var res 	= 	checkDate($('#date_yyyy'),$('#date_mm'),$('#date_dd'), true, '', 'status_message', false);
			if(!res){	
				$('#status_message').show();
				return ;
			}	
			listAllRecords();				
		});
	});	
	
	function listAllRecords() {
		$('#detail_content').hide();
		$('#multi_payment_content').hide();
		var dateData = "date_dd=" + $('#date_dd').val() + "&date_mm=" + $('#date_mm').val() + "&date_yyyy=" + $('#date_yyyy').val()
		$.ajax({
				type:	"GET",
				url:	"../PaymentCollection/GetAllRecords?set_ajax_view",
				data: 	dateData,
				beforeSend: function() {
					$('#duplicated_record').hide();
					$('#barLoading').show();
				},
				complete: function() {
					$('#duplicated_record').show();
					$('#barLoading').hide();
				},
				success: function(data) {
					if (data == false) {
						var buttons = {};
						buttons[_('Close')] = function() {
								$(this).dialog('close');		
							};
						generalDialog(null,null,_("Invalid date"),'warn',{
								autoOpen: true,			
								bgiframe: true,
								resizable: true,
								modal: true,							
								buttons: buttons
							});
						return;
					}
					$('#ticket_content').html(data).show();
					$('#payment_details tbody tr:even').toggleClass('alt');
				}				
			}); 
	}
	
	function viewDetails(paymentCollectionId, patientId, count, selectedDate) {
		if (count == 1) {
			$.ajax({
				url: 	"../PaymentCollection/ViewItemDetails?set_ajax_view",
				data:	"payment_collection_id=" + paymentCollectionId,
				success: function(data) {
					$('#detail_content').html(data).show();
					$('#ticket_content').hide();
					$('#multi_payment_content').hide();
				}
						
			});
		} else {
			viewMultiPayment(paymentCollectionId, patientId, selectedDate);
		}		
	}
	
	function viewMultiPayment(paymentCollectionId, patientId, selectedDate) {
		$('#ticket_content').hide();
		$('#detail_content').hide();
		$.ajax({
				url: 	"../PaymentCollection/ViewMultiItemDetails?set_ajax_view",
				data:	"payment_collection_id=" + paymentCollectionId +
						"&patient_id=" + patientId +
						"&selected_date=" + selectedDate,
				success: function(data) {
					$('#multi_payment_content').html(data).show();
					$('#ticket_content').hide();
					$('#detail_content').hide();
				}
						
			});		
	}
	
	function turnBack(src, dest) {
		$('#' + src).hide();			
		listAllRecords();	
	}
	
	function savePayment(paymentCollectionId) {
		var amount = $.trim($('#real_payment').val());		
		if (!amount.match(/^[0-9]+$/)) {
			var buttons = {};
			buttons[_('Close')] = function() {
					$(this).dialog('close');		
				};
			generalDialog(null,null,_("Invalid real payment"),'warn',{
					autoOpen: true,			
					bgiframe: true,
					resizable: true,
					modal: true,							
					buttons: buttons
				});
			return;
		}
		
		$.ajax({
				type: 	"POST",
				url: 	"../PaymentCollection/SavePayment?set_ajax_view",
				data:	"payment_collection_id=" + paymentCollectionId +
						"&amount=" + amount +
						"&note=" + $('#note').val(),
				success: function(data) {
					$('#status_message').text(_('Saving succeed'));
					listAllRecords();	
				}					
			});
	}
	
	
	